package com.xueyu.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xueyu.dao.ExceptDao;
import com.xueyu.dao.personinfoDao;
import com.xueyu.domain.Except.ExceptBean;
import com.xueyu.domain.personinfo.personinfoBean;
import com.xueyu.util.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/excepts")
public class ExceptContoller {

    @Autowired
    ExceptDao exceptDao;

    @Autowired
    personinfoDao personinfoDao;

    @RequestMapping("/add")
    public ResultBean add(@RequestBody ExceptBean exceptBean) throws Exception{
        if (exceptBean.getExceptId() == 0){
            exceptDao.insert(exceptBean);
        }else{
            exceptDao.updateById(exceptBean);
        }
        return new ResultBean();
    }

    @RequestMapping("/list")
    public ResultBean list(@RequestBody Map map){
        int curPage = (Integer) map.get("curPage");
        int size = (Integer) map.get("size");
        Page<ExceptBean> pager = new Page<ExceptBean>(curPage,size);
        List<ExceptBean> exceptList = exceptDao.listPage(pager,map);
        pager.setRecords(exceptList);
        List<personinfoBean> personinfoList = personinfoDao.selectList(null);
        ResultBean resultBean = new ResultBean();
        resultBean.put("pager",pager);
        resultBean.put("personinfoList",personinfoList);
        return resultBean;
    }

    @RequestMapping("/del/{exceptId}")
    public ResultBean del(@PathVariable("exceptId") int exceptId){
        exceptDao.deleteById(exceptId);
        return new ResultBean();
    }

    @RequestMapping("/getId/{exceptId}")
    public ResultBean getId(@PathVariable("exceptId") int exceptId){
        ResultBean resultBean = new ResultBean();
        Map map = new HashMap();
        if(exceptId > 0){
            ExceptBean except = exceptDao.selectById(exceptId);
            map.put("except",except);
        }
        List<personinfoBean> personinfoList = personinfoDao.selectList(null);
        map.put("personinfoList",personinfoList);
        resultBean.put("data",map);
        return resultBean;
    }
}
